package com.xiaogao.serviceEdu.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.xiaogao.commonUtils.Result;
import com.xiaogao.serviceEdu.entity.EduChapter;
import com.xiaogao.serviceEdu.entity.vo.ChapterVo;
import com.xiaogao.serviceEdu.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程章节 前端控制器
 * </p>
 *
 * @author 小高
 * @since 2021-06-08
 */
@Api(description = "课程章节管理")
@RestController
@RequestMapping("/serviceEdu/chapter")
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    @ApiOperation(value = "获取章节课时数据列表")
    @GetMapping("/getChapterVideo/{courseId}")
    public Result getChapterVideo(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId) {

        List<ChapterVo> chapterVoList = chapterService.getChapterVideoByCourseId(courseId);
        return Result.ok().data("items", chapterVoList);
    }

    @ApiOperation(value = "新增章节")
    @PostMapping("/addChapter")
    public Result addChapter(
            @ApiParam(name = "eduChapter", value = "章节对象", required = true)
            @RequestBody EduChapter eduChapter) {
        chapterService.save(eduChapter);
        return Result.ok();
    }

    @ApiOperation(value = "根据id查询章节")
    @GetMapping("/getChapterById/{id}")
    public Result getChapterById(
            @ApiParam(name = "id",value = "章节ID",required = true)
            @PathVariable String id){
        EduChapter eduChapter = chapterService.getById(id);
        return Result.ok().data("item",eduChapter);
    }

    @ApiOperation(value = "根据id修改章节信息")
    @PutMapping("/updateChapter/{id}")
    public Result updateChapter(
            @ApiParam(name = "eduChapter",value = "章节对象",required = true)
            @RequestBody EduChapter eduChapter,

            @ApiParam(name = "id",value = "章节id",required = true)
            @PathVariable String id){
        eduChapter.setId(id);
        chapterService.updateById(eduChapter);
        return Result.ok();
    }

    @ApiOperation(value = "根据id删除章节")
    @DeleteMapping("/removeChapter/{id}")
    public Result removeChapter(
            @ApiParam(name = "id",value = "章节id",required = true)
            @PathVariable String id){
        boolean flag = chapterService.removeChapterById(id);
        if (flag){
            return Result.ok();
        }else{
            return Result.error().message("删除失败！");
        }
    }

}

